package com.hfut.eeg.service;

import com.hfut.eeg.dto.LoginResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.service
 * @Description: TODO
 * @date 2019-11-08 16:59
 */
@Component("userdetailService")
public class AdminUserdetailService implements UserDetailsService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LoginService loginService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        com.hfut.eeg.dto.User result = loginService.getUserByUserName(s);
        //System.out.println("loadUserByUsername:"+result);
        //判断权限，赋予角色
        String authority = "ROLE_USER";
        if(result.getAuthority() == 1){
            authority = "ROLE_ADMIN";
        }
        //System.out.println(authority);
        return new User(result.getUserName(),result.getUserPassword(),
                AuthorityUtils.commaSeparatedStringToAuthorityList(authority));
    }
}
